<template>
  <el-dialog :visible.sync="addVisible" @close="closeSelf" width="70%" :show-close='false' class="opinionDialog" :closeOnClickModal='false'>
    <span slot="title" class="dialog-title">
		  	检测单{{checkOrReview == 'check' ?'校核':'审核'}}
			<el-button  size="mini" @click="closeSelf" class="popupClose">关闭</el-button>
		</span>
    <div class="opinionCon">
      <div class="penalrow selectw100">
        <div class=" width100 ">
          <div class="inlineblock verticalmiddle">
            是否通过
          </div>
          <div class="inlineblock verticalmiddle informationItem">
            <el-select v-model="isPassValue" placeholder="请选择" size="mini">
              <el-option v-for="item in isPassOption" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
      </div>
      <div class="penalrow selectw100" v-if="isPassValue=='2'">
        <div class=" width100 ">
          <div class="inlineblock verticalmiddle">
            驳回意见
          </div>
          <div class="inlineblock verticalmiddle informationItem">
            <!-- opinion-->
            <el-input type="textarea"  autosize  placeholder="请输入内容"  v-model="panelOpinion" @keyup.native="$event.target.value = $event.target.value.replace(/^\s+|\s+$/gm,'')">
            </el-input>
          </div>
        </div>
      </div>
      <div v-if="isPassValue=='2'" style="height:calc(100% - 145px);margin-top:20px;">
        <!--<div class="inlineblock">样品</div>-->
        <el-table :data="sampleData" style="width: 100%;height:100%;" size="mini" class="tableCon" @header-click="taskTableHeadClick"
          cell-class-name="batchtablecell" highlight-current-row>
          <el-table-column label="序号" width="50">
            <template slot-scope="scope">
              {{scope.$index+1}}
            </template>
          </el-table-column>
          <el-table-column property="sampleCode" label="样品编号">
          </el-table-column>
          <el-table-column property="sampleName" label="样品名称" show-overflow-tooltip>
          </el-table-column>
          <el-table-column property="chonglu" label="重录"  v-if = "reload">
            <template slot-scope="scope">
              <!--<div @click.stop="clickCheckBox(scope.row,'chonglu')">-->
                <el-checkbox v-model="scope.row.chonglu" @change = "clickCheckBox($event,scope.row,'chonglu')">重录</el-checkbox>
              <!--</div>-->
            </template>
          </el-table-column>
          <!--如果是校核 审核人为空时 冲采和重测出现-->
          <el-table-column property="chongce" label="重测" v-if="checkOrReview == 'review' || (checkOrReview == 'check' && testEntryData.auditor == undefined) || (checkOrReview == 'check' && testEntryData.auditor == null)">
            <template slot-scope="scope">
              <!--<div @click.stop="clickCheckBox(scope.row,'chongce')"  id = "chongce">-->
                <el-checkbox v-model="scope.row.chongce" :disabled = "isDisabled || scope.row.space == 2" @change = "clickCheckBox($event,scope.row,'chongce')">重测</el-checkbox>
              <!--</div>-->
            </template>
          </el-table-column>
          <!--testEntryData-->
          <el-table-column property="chongcai" label="重采" v-if="checkOrReview == 'review' || (checkOrReview == 'check' && testEntryData.auditor == undefined) || (checkOrReview == 'check' && testEntryData.auditor == null)">
            <template slot-scope="scope">
              <!--<div @click.stop="clickCheckBox(scope.row,'chongcai')">-->
                <el-checkbox v-model="scope.row.chongcai" :disabled = "isDisabled || scope.row.space == 2" @change = "clickCheckBox($event,scope.row,'chongcai')">重采</el-checkbox>
              <!--</div>-->
            </template>
          </el-table-column>
          <el-table-column label="意见" property="opinion">
            <template slot-scope="scope">
              <el-input size="mini" v-model="scope.row.opinion"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
			<el-button class="penalButton" type="primary" size="mini" @click="submitCheck" :disabled = "!isRepeat">确定</el-button>
      <el-button class="penalButton" size="mini" v-on:click="closeSelf">取消</el-button>
		</span>
  </el-dialog>
</template>

<script>
  import { mapState } from 'vuex'
  import $ from 'jquery'

  export default {
    data() {
      const generateData = _ => {
        const data = [];
        for (let i = 1; i <= 15; i++) {
          data.push({
            key: i,
            label: `标准溶液 ${i}`
          });
        }
        return data;
      };
      return {
        yesornoValue: '1',
        enableStatusValue: '1',
        monitoringPoints: generateData(),
        monitoringPointsValue: [1, 4],
        isPassValue: '1',
        isPassOption: [
          {
            value: '1',
            label: '通过'
          },
          {
            value: '2',
            label: '驳回'
          }
        ],
        sampleData: [],
        tableDataWaitChose: [],
        chongceflag: false,
        chongluflag: false,
        chongcaiflag: false,
        isRepeat: true,
        receptModuleId: "",
        isDisabled:false, //驳回的时候 一行数据选中了重录 所有行的重采和重测都不可用
        reload:true,
        batchData:{},
        panelOpinion:"",
      }
    },
    computed: {
      ...mapState(['enableStatus', 'tableHeightA', 'yesorno', 'enableStatus']),
    },
    watch: {
      addVisible(val, oldVal) {
        if (val) {
          this.sampleData = this.sampleInfo
          for (var i in this.sampleData) {
            this.$set(this.sampleData[i], 'chongce', false);
            this.$set(this.sampleData[i], 'chonglu', false);
            this.$set(this.sampleData[i], 'chongcai', false);
            this.$set(this.sampleData[i], 'opinion', null);
          }
          console.log(this.testEntryData)
          this.batchData = JSON.parse(JSON.stringify(this.testEntryData))
          if (this.checkOrReview == 'check') {
            this.receptModuleId = this.util.getModuleId({ module: "mygroupsanjishenhe" })
          }
          this.isDisabled = false;

        }
      },
      panelOpinion(val){
          var val1 = this.trimStr(val);
          if(val1 != ''){ //如果驳回意见不为空 重测和重采不能勾选
             this.isDisabled = true;
          }else{ //如果驳回意见为空   判断有没有被勾中的重录，有选中的重录，重测和重采不能勾选
            var flag = false
            for(var i in this.sampleData){
              if(this.sampleData[i].chonglu){
                flag = true
                break;
              }else{
                flag = false
              }
            }
            if(flag){
              this.isDisabled = true;
            }else{
              this.isDisabled = false;
            }
          }
      }
    },
    props: ['sampleInfo', 'canEdit', 'jiaoheShow', 'addVisible', 'checkOrReview', 'testEntryData'],
    methods: {
      trimStr(str){
        return str.replace(/(^\s*)|(\s*$)/g,"");
      },
      close() {
        this.$emit('close');
        this.isPassValue = '1';
        this.sampleData = [];
        this.isRepeat = true;
      },
      closeSelf() {
        this.$emit('update:addVisible', false);
        this.isPassValue = '1'
        this.sampleData = [];
        this.isRepeat = true;
        this.panelOpinion = "";
      },
      submitCheck() {
        if (this.isPassValue == '1') {
          this.batchData.isPass = true;
          for (var i in this.sampleData) {
            this.sampleData[i].rejectFlag = 0;
            this.sampleData[i].moduleId = this.receptModuleId;
            this.sampleData[i].operation = 'check';
          }
        } else {
          var flag = false;
          for (var i in this.sampleData) {
            if (this.sampleData[i].chonglu) { //重录传rejectFlag为1
              this.sampleData[i].rejectFlag = 1;
              flag = true;
            } else if (this.sampleData[i].chongce) { //重测传rejectFlag为2
              this.sampleData[i].rejectFlag = 2;
              flag = true;
            } else if (this.sampleData[i].chongcai) {//重采传rejectFlag为3
              this.sampleData[i].rejectFlag = 3;
              flag = true;
            } else {
              this.sampleData[i].rejectFlag = 0;
            }
            if (this.checkOrReview == 'check') {
              this.sampleData[i].moduleId = this.receptModuleId;
              this.sampleData[i].operation = 'check';
            }
          }
          var panelOpinion = this.trimStr(this.panelOpinion)
          if (!flag && panelOpinion == '') {
            this.$message({
              type: 'info',
              message: '请填写驳回原因或者选择需要驳回样品！'
            })
            return;
          }
          this.batchData.isPass = false;
        }

        if (this.checkOrReview == "check") {
          var url = '/detection/checkBatch.htm';

        } else {
          var url = '/detection/reviewBatch.htm';
        }
        this.isRepeat = false;

        this.sampleData[0].batchCode = this.testEntryData.code

        this.batchData.sampleBeans = this.sampleData;
        this.batchData.opinion = this.panelOpinion;
        this.$http({
          method: 'post',
          apiUrl: url,
          params: this.batchData,
          isJson: true,
          isLoading:true
        }).then(res => {
          this.isRepeat = true;
          if (res.code === 0) {

            if (this.checkOrReview == 'check') {  //校核页面如果审核人为空了 调接口改变任务状态
              if(this.testEntryData.auditor == null || this.testEntryData.auditor == "" || this.testEntryData.auditor == undefined){
                this.updateTaskStatus(this.sampleData)
                // this.addOriginal()
              }
            }
            if (this.checkOrReview == 'review') { //审核都要调
              this.updateTaskStatus(this.sampleData)
              // this.addOriginal() //
            }
            this.$parent.$parent.getTestBatchs(this.$parent.$parent.testItemQuery)
						this.$store.dispatch('getWorkflowList',0).then(res=>{
							this.$notify({
                showClose: true,
                message: '操作成功',
                type: 'success',
                title:"成功"
              })
						})
            this.closeSelf();
            this.close();
          }
        })

      },
      // 原始记录相关 审核成功调用
      addOriginal(){
         this.$http({
          method: 'post',
          apiUrl: "/original/addOriginal.htm",
          params: this.batchData,
          isJson: true,
        }).then(res => {
           if(res.code == 0){

           }
        })
      },
      //改变任务状态
      updateTaskStatus(data) {
        console.log(data);
        this.$http({
          method: 'post',
          apiUrl: '/detection/updateTaskByTestItem.htm',
          params: data,
          isJson: true,
        }).then(res => {
          if (res.code === 0) { //防止异步

          }
        })
      },
      taskTableHeadClick(column) {
        if (column.property != 'chongce' && column.property != 'chonglu' && column.property != 'chongcai') {
          return
        }
        var flag;
        if (column.property == 'chongce') {
          this.chongceflag = !this.chongceflag;
          flag = this.chongceflag;
        } else if (column.property == 'chonglu') {
          this.chongluflag = !this.chongluflag;
          flag = this.chongluflag;
        } else {
          this.chongcaiflag = !this.chongcaiflag;
          flag = this.chongcaiflag;
        }
        for (var i = 0; i < this.tableDataWaitChose.length; i++) {
          this.tableDataWaitChose[i].chongce = false;
          this.tableDataWaitChose[i].chonglu = false;
          this.tableDataWaitChose[i].chongcai = false;
          this.tableDataWaitChose[i][column.property] = flag;
        }
      },
      // clickCheckBox(row, key) {
      //   if (!row[key]) {
      //     this.$set(row, 'chongce', false);
      //     this.$set(row, 'chonglu', false);
      //     this.$set(row, 'chongcai', false);
      //     this.$set(row, row[key], true);
      //   }
      // },
      clickCheckBox(val,row, key) {
        // 如果选中重录，其他样品的重测和重采要清空，即一个选择重录 其他都不能选择
        if(row[key] && key == 'chonglu'){
          for(var i in this.sampleData){
            this.sampleData[i].chongce = false;
            this.sampleData[i].chongcai = false;
          }
        }
        // 一个样品只能勾选一个
        if (row[key]) {
          row.chongce = false;
          row.chonglu = false;
          row.chongcai = false;
          row[key] = true;
        }
        // 一个样品选中重录后，重测和重采都不可用
        if(row.chonglu){ //如果勾选的重录，则其他不可选，取消勾选的话去判断其他样品

          this.isDisabled = true;
        }else{
          var panelOpinion = this.trimStr(this.panelOpinion)
          if(panelOpinion == '' || panelOpinion == null){  //取消重录的勾选 判断驳回意见是不是为空，为空了才可用重测重采
            var flag = false
            for(var i in this.sampleData){
              if(this.sampleData[i].chonglu){
                flag = true
                break;
              }else{
                flag = false
              }
            }
            if(flag){
              this.isDisabled = true;
            }else{
              this.isDisabled = false;
            }
          }else{
            this.isDisabled = true;
          }
        }


      },
    }
  }

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
  .opinionDialog .el-dialog {
    margin-top: 10vh !important;
    height: 80%;
  }

  .opinionDialog .el-dialog .el-dialog__body {
    height: calc(100% - 85px);
    padding: 10px;
  }

  .opinionDialog .opinionCon {
    height: 100%
  }
</style>
